package com.gzx.plugin.tjzy.xxljob;

import com.gzx.plugin.tjzy.modular.call.pre.service.GzxPreOutCallService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import vip.xiaonuo.common.annotation.CommonCheckInstanceOnline;
import vip.xiaonuo.common.tenant.TenantContext;
import vip.xiaonuo.common.util.CommonDateUtil;

/**
 * @author zjt
 * @description
 * @date 2025/1/7 16:08
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class PreOutCallXxlJob {

    private final GzxPreOutCallService preOutCallService;

    @XxlJob("preOutCallJobHandler")
    @CommonCheckInstanceOnline
    public void preOutCallJobHandler() {
        log.info("XxlJob定时任务启动--------------->{}", CommonDateUtil.getCurrentTimeStr());
        log.info("预测外呼任务完结");
        try {
            TenantContext.disableTenantFilter();
            preOutCallService.finishCallTask(null);
        } catch (Exception e) {
            log.error("预测外呼任务出现异常", e);
        } finally {
            TenantContext.enableTenantFilter();
            TenantContext.clear();
        }
    }

    @XxlJob("adjustCompletedJobHandler")
    @CommonCheckInstanceOnline
    public void adjustCompletedJobHandler() {
        log.info("XxlJob定时任务启动--------------->{}", CommonDateUtil.getCurrentTimeStr());
        log.info("校准预测外呼已完成数量");
        try {
            TenantContext.disableTenantFilter();
            preOutCallService.adjustCompleted();
        } catch (Exception e) {
            log.error("校准预测外呼已完成数量出现异常", e);
        } finally {
            TenantContext.enableTenantFilter();
            TenantContext.clear();
        }
    }
}